package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.DictUtils;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.mapper.DictDataMapper;
import com.ruoyi.system.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, SysDictData> implements DictDataService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public IPage<SysDictData> selectDictDataList(Page<SysDictData> page, SysDictData dictData) {
        LambdaQueryWrapper<SysDictData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(dictData.getDictType()),SysDictData::getDictType,dictData.getDictType())
                .like(StringUtils.isNotBlank(dictData.getDictLabel()),SysDictData::getDictLabel,dictData.getDictLabel())
                .eq(StringUtils.isNotBlank(dictData.getStatus()),SysDictData::getStatus,dictData.getStatus())
                .orderByAsc(SysDictData::getDictSort);
        return baseMapper.selectPage(page,wrapper);
    }

    @Override
    public int insertDictData(SysDictData dict) {
        int row = baseMapper.insert(dict);
        if (row > 0) {
            List<SysDictData> dictDatas = baseMapper.selectList(
                    Wrappers.<SysDictData>lambdaQuery()
                            .eq(SysDictData::getDictType,dict.getDictType())
                            .eq(SysDictData::getStatus,"0")
                            .orderByAsc(SysDictData::getDictSort));
            DictUtils.setDictCache(dict.getDictType(), dictDatas);
        }
        return row;
    }

    @Override
    public List<SysDictData> selectByDictType(String dictType) {
        if (Boolean.TRUE.equals(redisTemplate.hasKey(dictType))){
            System.out.println("拿取缓存数据key:"+dictType);
            return redisTemplate.opsForList().range(dictType, 0, -1);
        }
        if (dictType.equals("store_type")){
            List<SysDictData> sysDictDataList = baseMapper.selectList(Wrappers.<SysDictData>lambdaQuery().select(SysDictData::getDictCode, SysDictData::getDictLabel).eq(SysDictData::getDictType, "src_storage_type"));
            for (SysDictData dictData : sysDictDataList) {
                if (dictData.getDictLabel().equals("农产品")){
                    List<SysDictData> sysDictData1 = baseMapper.selectList(Wrappers.<SysDictData>lambdaQuery().select(SysDictData::getDictCode, SysDictData::getDictLabel).eq(SysDictData::getDictType, "src_crop_type"));
                    dictData.setChildren(sysDictData1);
                }else if (dictData.getDictLabel().equals("加工产品")){
                    List<SysDictData> sysDictData1 = baseMapper.selectList(Wrappers.<SysDictData>lambdaQuery().select(SysDictData::getDictCode, SysDictData::getDictLabel).eq(SysDictData::getDictType, "src_task_type"));
                    dictData.setChildren(sysDictData1);
                }
            }
            redisTemplate.opsForList().leftPushAll(dictType,sysDictDataList);
            return sysDictDataList;
        }
        List<SysDictData> sysDictDataList = baseMapper.selectList(Wrappers.<SysDictData>lambdaQuery().select(SysDictData::getDictCode, SysDictData::getDictLabel).eq(SysDictData::getDictType, dictType));
        redisTemplate.opsForList().leftPushAll(dictType,sysDictDataList);
        return sysDictDataList;
    }
}
